package creationalPatterns.singletonPattern;


//懒汉式
public class LazySingleton {
    private static LazySingleton instance;
    public static synchronized LazySingleton getInstance(){
        if (instance == null) {
            instance = new LazySingleton();
        }
        return instance;
    }
}
//饿汉式
class HungrySingleton {
    private static HungrySingleton instance = new HungrySingleton();
    private HungrySingleton (){}
    public static synchronized HungrySingleton getInstance() {
        return instance;
    }
}

//双检锁/双重校验锁
class DoubleSingleton {
    private volatile static DoubleSingleton singleton;
    private DoubleSingleton (){}
    public static DoubleSingleton getSingleton() {
        if (singleton == null) {
            synchronized (DoubleSingleton.class) {
                if (singleton == null) {
                    singleton = new DoubleSingleton();
                }
            }
        }
        return singleton;
    }
}

//登记式/静态内部类
class StaticSingleton {
    private static class SingletonHolder {
        private static final StaticSingleton INSTANCE = new StaticSingleton();
    }
    private StaticSingleton (){}
    public static final StaticSingleton getInstance() {
        return SingletonHolder.INSTANCE;
    }
}

//枚举式
enum EnumSingleton {
    INSTANCE;
    public void whateverMethod() {
    }
}

//最后总结，一般用饿汉式